<?php defined('InShopNC') or exit('Access Invalid!');?>
<script src="<?php echo RESOURCE_SITE_URL;?>/js/jquery.ajaxContent.pack.js"></script>
<script src="<?php echo RESOURCE_SITE_URL;?>/js/jquery-ui/i18n/zh-CN.js"></script>
<script src="<?php echo RESOURCE_SITE_URL;?>/js/common_select.js"></script>
<script src="<?php echo SHOP_RESOURCE_SITE_URL;?>/js/store_goods_add.step2.js"></script>
<script type="text/javascript" src="<?php echo RESOURCE_SITE_URL;?>/js/fileupload/jquery.iframe-transport.js" charset="utf-8"></script>
<script type="text/javascript" src="<?php echo RESOURCE_SITE_URL;?>/js/fileupload/jquery.ui.widget.js" charset="utf-8"></script>
<script type="text/javascript" src="<?php echo RESOURCE_SITE_URL;?>/js/fileupload/jquery.fileupload.js" charset="utf-8"></script>
<script type="text/javascript" src="//cdn.bootcss.com/json2/20150503/json2.min.js" charset="utf-8"></script>
<script src="<?php echo RESOURCE_SITE_URL;?>/js/jquery.poshytip.min.js"></script>
<link rel="stylesheet" type="text/css" href="<?php echo RESOURCE_SITE_URL;?>/js/jquery-ui/themes/ui-lightness/jquery.ui.css"  />
<div class="ncsc-index">
  <div class="top-container">
    <div class="basic-info">
        <dl class="ncsc-seller-info">
          <dd><span class="import_input">选择csv文件数据：<input type="file" name="csv" id="import_csv"></span><span class="tips"></span></dd>
        </dl>
    </div>
  </div>
  <div class="seller-cont">

  </div>
</div>
<script>
$(function(){
  /* 商品图片ajax上传 */
  var importFile= $('#import_csv'),updateInter;
  importFile.fileupload({
    dataType: 'html',
    url: SITEURL + '/index.php?act=store_goods_add&op=import&nc_type=uploadedfile',
    formData: {name:''},
    add: function (e,data) {
      //检查文件格式
      file=data.originalFiles;
      if(data.originalFiles[0].name.substr(data.originalFiles[0].name.length-4).toLowerCase()!='.csv'){
        alert('你选择的文件不是csv格式');
        return false;
      }else if(data.originalFiles[0].size<0){
        alert('你选择的是空文件');
        return false;
      }
      $('.import_input').hide();var point=0;
      var tips=$(".tips");
      tips.text('正在上传请稍后');
      updateInter=setInterval(function(){
        if(point){
          if(point==1){
            tips.text('正在上传请稍后..');
            point=2;
          }else if(point==2){
            tips.text('正在上传请稍后...');
          }else{
            tips.text('正在上传请稍后');
            point=0;
          }
        }else{
          tips.text('正在上传请稍后.');
          point=1;
        }
      },3000);
      data.submit();
    },
    done: function (e,data) {
      clearInterval(updateInter);
      $(".tips").text('上传处理完成');
//      $('.import_input').show();
      $(".seller-cont").html(data.result);
    }
  });
});
/**
 * 删除列
 * @param index
 * @returns {boolean}
 */
  function delTableCel(index){
    var importTable=$("#import_data");
    importTable.find("tr").each(function(i,tr){
      $(tr).find("td:eq("+index+")").remove();
      $(tr).find("th:eq("+index+")").remove();
    });
    return false;
  }
function delTableRow(id){
  $('#'+id).remove();
  return false;
}
/**
 * 开始导入商品
 */
  function importGoods(){
  var import_button=$("#import_button"),importTime= 0;
  import_button.html('正在导入 已用时 0 秒 ').attr('disabled',true);
  var importInter=setInterval(function(){
    importTime++;
    import_button.html('正在导入 已用时 '+importTime+' 秒 ');
  },1000);
    var goods_field_index=$("#goods_field_index>th"),fields={},goods_name_index=0;
    goods_field_index.each(function(i,th){
      var index=$(th).find('select').val();
      if(i&&index){
        if(index=='goods_name'){
            goods_name_index=i;
        }
        fields[i]=index.split('.');
      }
    });
  var goodsList=$("#import_data>tbody>tr"),goodsData=[];
  goodsList.each(function(i,tr){
    var data={},trId=tr.getAttribute('id');
    $.each(fields,function(index,name){
      var val=$("#"+trId+"_"+index).val();
      if(name.length==1){
        data[name[0]]=val;
      }else{
        if(name[0] in data==false){
          data[name[0]]={};
        }
        data[name[0]][name[1]]=val;
      }
    });
    goodsData.push(data);
  });
  //向服务器提交数据
  $.post(SITEURL + '/index.php?act=store_goods_add&op=import&nc_type=import',{list:JSON.stringify(goodsData)},function(data){
    clearInterval(importInter);
    if(data.status){
      var num=0;
     $.each(data.list,function(id,item){
       var node=$("#goods_"+id);
       if(item.has){
         node.remove();
       }else{
         var goods_name=$("#goods_"+id+"_"+goods_name_index);
         node.find('td:eq('+goods_name_index+')').html('<a href="'+item.url+'" target="_blank">'+goods_name.val()+'</a>');
       }
       num++;
     });
      alert('共导入'+num+'个商品,用时'+importTime+'秒');
    }else{
      alert(data.message);
    }
    import_button.html('字段设定完成开始导入');
    $("#import_button").attr('disabled',false);

  },'json');
  }
</script>
